using System;
using GoldStone.Split;

namespace GoldStone.HLSplit {
    public class HLSplitter : Splitter {
        private HLSplitLib impl;

        public HLSplitter(String dictFileFolder) {
            impl = new HLSplitLib(dictFileFolder);
        }

        public HLSplitter(String dictFileFolder, String userDictFullPath) {
            impl = new HLSplitLib(dictFileFolder, userDictFullPath);
        }

        public static NatureFlag convert(WordNature nature) {
            if (nature == WordNature.Adj) {
                return NatureFlag.NATURE_D_A;
            } else if (nature == WordNature.Adv) {
                return NatureFlag.NATURE_D_D;
            } else if (nature == WordNature.All) {
                return NatureFlag.NATURE_D_All;
            } else if (nature == WordNature.Aux) {
                return NatureFlag.NATURE_D_U;
            } else if (nature == WordNature.Conj) {
                return NatureFlag.NATURE_D_C;
            } else if (nature == WordNature.Noun) {
                return NatureFlag.NATURE_D_F | NatureFlag.NATURE_D_N | NatureFlag.NATURE_A_NR | NatureFlag.NATURE_A_NS
                       | NatureFlag.NATURE_A_NT | NatureFlag.NATURE_A_NZ;
            } else if (nature == WordNature.Prep) {
                return NatureFlag.NATURE_D_P;
            } else if (nature == WordNature.Verb) {
                return NatureFlag.NATURE_D_V;
            }
            return NatureFlag.NATURE_D_NOTHING;
        }

        public static WordNature convert(NatureFlag nature) {
            if (nature == NatureFlag.NATURE_D_A) {
                return WordNature.Adj;
            } else if (nature == NatureFlag.NATURE_D_D) {
                return WordNature.Adv;
            } else if (nature == NatureFlag.NATURE_D_All) {
                return WordNature.All;
            } else if (nature == NatureFlag.NATURE_D_U) {
                return WordNature.Aux;
            } else if (nature == NatureFlag.NATURE_D_C) {
                return WordNature.Conj;
            } else if ((nature & (NatureFlag.NATURE_D_F | NatureFlag.NATURE_D_N | NatureFlag.NATURE_A_NR | NatureFlag.NATURE_A_NS | NatureFlag.NATURE_A_NT | NatureFlag.NATURE_A_NZ)) != 0) {
                return WordNature.Noun;
            } else if (nature == NatureFlag.NATURE_D_P) {
                return WordNature.Prep;
            } else if (nature == NatureFlag.NATURE_D_V) {
                return WordNature.Verb;
            }
            return 0;
        }

        public static WordUnit convert(SHLSegWord hlword) {
            WordUnit word = new WordUnit();
            unsafe {
                word.Word = new String(hlword.s_szWord);
            }
            word.Nature = convert((NatureFlag) hlword.s_dwPOS);
            return word;
        }

        public string[] Split(string text) {
            SHLSegWord[] hlwords = impl.Split(text);
            String[] result = new string[hlwords.Length];
            unsafe {
                for (int i = 0; i < hlwords.Length; i++) {
                    result[i] = new String(hlwords[i].s_szWord);
                }
            }
            return result;
        }

        public WordUnit[] SplitWithNatureInfo(string text) {
            SHLSegWord[] hlwords = impl.SplitWithNatureInfo(text);
            WordUnit[] words = new WordUnit[hlwords.Length];
            for (int i = 0; i < words.Length; i++) {
                words[i] = convert(hlwords[i]);
            }
            return words;
        }
    }
}